package com.company.linkedList;

public class DynamicNums {
    public int getDecimalValue(ListNode head) {
        ListNode newHead = revertList(head);
        return parseInt(newHead);
    }


    private int parseInt(ListNode head){
        int value = 0;
        ListNode node=head;
        int i = 0;
        while ( node!=null){
            if (node.val != 0) {
                value+=Math.pow(2,i);
            }
            node = node.next;
            i++;
        }
        return value;
    }

    private ListNode revertList(ListNode head) {
        ListNode newHead = null;
        ListNode node = null;
        ListNode next = null;
        //移动到新节点上
        newHead = head;
        node = head.next;
        newHead.next = null;
        //记录当前操作元素
        while (node != null) {
            next = node.next;
            node.next = newHead;

            newHead = node;
            node = next;
        }
        return newHead;
    }
}
